package com.yuju.controller.group;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.ProtocolCustomReturnCodeEnum;
import com.yuju.group.model.ProtocolCustomModel;
import com.yuju.group.service.IProtocolCustomService;
import com.yuju.util.Utils;
import com.yuju.vo.group.ProtocolCustomVO;

/**
 * 类名：ProtocolCustomController  <br />
 *
 * 功能：协议单位接口
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月24日 上午9:40:37  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("protocolCustom")
public class ProtocolCustomController extends BaseController<ProtocolCustomVO,ProtocolCustomModel> {
	
	@Autowired
	private IProtocolCustomService protocolCustomService;
	
	/**
	 * 功能：查询名称重复<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月8日 下午2:44:16 <br/>
	 */
	@RequestMapping("validateName")
	@ResponseBody
	public Result validateName(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		protocolCustomVO.setHotelId(null);
		boolean validateName = protocolCustomService.validateName(convert(protocolCustomVO));
		Result result = new Result();
		if(validateName){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：查找列表<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:01:20 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		protocolCustomVO.setHotelId(null);
		Map<String, String> fail = FieldValidator.validateAll(protocolCustomVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = protocolCustomService.pageList(convert(protocolCustomVO), protocolCustomVO.getPage(), protocolCustomVO.getPageSize());
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：查找单个<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:01:07 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		protocolCustomVO.setHotelId(null);
		Map<String, String> fail = FieldValidator.validateAll(protocolCustomVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		ProtocolCustomModel protocolCustomModel = protocolCustomService.find(convert(protocolCustomVO));
		if(Utils.isNotNull(protocolCustomModel)){
			result.setData(protocolCustomModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_FAILURE.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_QUERY_FAILURE.getDesc());	
		}
		return result;
	}
	
	/**
	 * 功能：更新<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月24日 下午2:00:56 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(protocolCustomVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		if(protocolCustomVO.getBillLimit().compareTo(new BigDecimal(0)) == 1){
			protocolCustomVO.setBillBalance(protocolCustomVO.getBillLimit());
		}else if(protocolCustomVO.getBillLimit().compareTo(new BigDecimal(-1))==0){
			protocolCustomVO.setBillBalance(new BigDecimal(0));
		}else{
			protocolCustomVO.setBillBalance(null);
		}
		Result result = new Result();
		int rows = protocolCustomService.updateSelective(convert(protocolCustomVO));
		if(rows>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_UPDATE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_UPDATE_FAILURE.getDesc());	
		}
		return result;
	}
	
	/**
	 * 功能：新增协议单位<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月23日 上午11:40:29 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(protocolCustomVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		if(protocolCustomVO.getBillLimit().compareTo(new BigDecimal(0)) == 1){
			protocolCustomVO.setBillBalance(protocolCustomVO.getBillLimit());
		}else if(protocolCustomVO.getBillLimit().compareTo(new BigDecimal(-1))==0){
			protocolCustomVO.setBillBalance(new BigDecimal(0));
		}else{
			protocolCustomVO.setBillBalance(null);
		}
		Result result = new Result();
		int rows = protocolCustomService.save(convert(protocolCustomVO));
		if(rows>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_SAVE_SUCCESS.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_SAVE_FAILURE.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：删除协议单位<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年11月23日 下午4:42:27 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj){
		ProtocolCustomVO protocolCustomVO = obj.toJavaObject(ProtocolCustomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(protocolCustomVO, "delete", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = protocolCustomService.del(protocolCustomVO.getId());
		if(rows>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_DEL_SUCCESS.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_DEL_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_DEL_FAILURE.getCode());
			result.setReturnMsg(ProtocolCustomReturnCodeEnum.PROTOCOLCUSTOM_DEL_FAILURE.getDesc());
		}
		return result;
	}
}
